﻿#include "unitrollmake.h"
#include "ui_unitrollmake.h"
#include"unitsearch.h"

#include<QtSql>
#include<QtGui>
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QSqlTableModel>
#include <QSqlError>
#include <QDebug>

unitrollmake::unitrollmake(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::unitrollmake)
{
    ui->setupUi(this);

//    wd=new Widget;
//    connect(ui->save,SIGNAL(clicked(bool)),wd,SLOT(showroll()));
//    UnitSearch *us = new UnitSearch;
//    connect(us, SIGNAL(sendData(QString)), this, SLOT(receiveData(QString)));
//    us->show();

    //选中整行+不可编辑
    ui->UnitInformation->setSelectionBehavior ( QAbstractItemView::SelectRows);
    ui->UnitInformation->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->UnitInformation->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->CadreInformation->setSelectionBehavior ( QAbstractItemView::SelectRows);
    ui->CadreInformation->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->CadreInformation->setEditTriggers(QAbstractItemView::NoEditTriggers);

    ui->UnitInformation->setColumnWidth(1,330);
    ui->UnitInformation->setColumnWidth(0,150);
    ui->UnitInformation->setColumnWidth(2,151);
    ui->UnitInformation->setColumnWidth(3,155);






}



void unitrollmake::receiveData(QString data)

{
       //获取传递过来的数据

    QString unitinfo=QString("select unitname,description,UnitOfCategoryName,CONCAT(adminestcount1,'人') from unit,UnitOfCategoryCode where unit.unitcategory=UnitOfCategoryCode.UnitOfCategoryId and unit.unitname='%1'").arg(data);
    QSqlQuery un;
    un.exec(unitinfo);
    while (un.next()) {
        //ui->UnitInformation->setRowCount(0);

        int rowcount=ui->UnitInformation->rowCount();
        ui->UnitInformation->insertRow(rowcount);
        QString p=un.value(0).toString();
        ui->UnitInformation->setItem(rowcount,0,new QTableWidgetItem(p));

        QString o=un.value(1).toString();
        ui->UnitInformation->setItem(rowcount,1,new QTableWidgetItem(o));

        QString i=un.value(2).toString();
        ui->UnitInformation->setItem(rowcount,2,new QTableWidgetItem(i));

        QString u=un.value(3).toString();
        ui->UnitInformation->setItem(rowcount,3,new QTableWidgetItem(u));

    }
    QString cadreinfo=QString::fromUtf8("select  CadreName,CadreInfo.idnum,postname,posttime,rankname,ranktime,birth,sex,AdminAreaCode.AdminArea ,joinworktime from unit,Post,RankCode, CadreInfo left join AdminAreaCode on CadreInfo.NativePlace=AdminAreaCode.AdminAreaID   where unit.unitid=CadreInfo.unitid and CadreInfo.IDNum=Post.IDNum and CadreInfo.RankID=RankCode.RankID and  unit.unitname='%1'").arg(data);

    QSqlQuery ci;
    ci.exec(cadreinfo);
    while(ci.next())
    {
        int rowcount=ui->CadreInformation->rowCount();
        ui->CadreInformation->insertRow(rowcount);
        QString p=ci.value(0).toString();
        ui->CadreInformation->setItem(rowcount,0,new QTableWidgetItem(p));

        QString o=ci.value(1).toString();
        ui->CadreInformation->setItem(rowcount,1,new QTableWidgetItem(o));

        QString i=ci.value(2).toString();
        ui->CadreInformation->setItem(rowcount,2,new QTableWidgetItem(i));

        QString u=ci.value(3).toString();
        ui->CadreInformation->setItem(rowcount,3,new QTableWidgetItem(u));
        QString a=ci.value(4).toString();
        ui->CadreInformation->setItem(rowcount,4,new QTableWidgetItem(a));

        QString b=ci.value(5).toString();
        ui->CadreInformation->setItem(rowcount,5,new QTableWidgetItem(b));

        QString e=ci.value(6).toString();
        ui->CadreInformation->setItem(rowcount,6,new QTableWidgetItem(e));


        QString d=ci.value(7).toString();
        if (d.toInt()==1) {
            ui->CadreInformation->setItem(rowcount,7,new QTableWidgetItem("男"));
        }
        if (d.toInt()==0) {
            ui->CadreInformation->setItem(rowcount,7,new QTableWidgetItem("女"));
        }




        QString f=ci.value(8).toString();
        ui->CadreInformation->setItem(rowcount,8,new QTableWidgetItem(f));
        QString g=ci.value(9).toString();
        ui->CadreInformation->setItem(rowcount,9,new QTableWidgetItem(g));
    }



}


unitrollmake::~unitrollmake()
{
    delete ui;
}

void unitrollmake::on_AddUnit_clicked()//转到槽
{
    UnitSearch *us = new UnitSearch;
    connect(us, SIGNAL(sendData(QString)), this, SLOT(receiveData(QString)));
    us->show();
}

void unitrollmake::on_save_clicked()//保存数据
{



    QString RollName=ui->RollName->text();
    if (!RollName.isEmpty()) {

    QString time =ui->RollBuildTime->text();
    QSqlQuery query;
    QString roll=QString::fromUtf8("insert into Roll values(null,'%2','%1',0)").arg(RollName).arg(time);
 //   query.exec(roll);
    qDebug()<<roll;
    int rollai;
    if (query.exec(roll)) {
        rollai=query.lastInsertId().toInt();

    }


    QString unitname;
    int i=ui->UnitInformation->rowCount();
    for (int a = 0; a < i; ++a)
    {
        unitname=ui->UnitInformation->item(a,0)->text();

        QSqlQuery qu;
        QString unit=QString::fromUtf8("insert into UnitRollRelation select null,unit.unitid,'%1' from unit where unitname='%2'").arg(rollai).arg(unitname);
        qu.exec(unit);
        qDebug()<<unit;
    }
    QString cadreidnum;
    int n=ui->CadreInformation->rowCount();
    for (int b = 0; b < n; ++b) {
        cadreidnum=ui->CadreInformation->item(b,1)->text();
        QSqlQuery cid;
        QString cadre=QString("insert into CadreRollRelation values(null,'%2','%1')").arg(rollai).arg(cadreidnum);
        cid.exec(cadre);

        qDebug()<<cadre;
    }






    QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("添加名册成功!"));
     emit ccc();

    QDialog:: reject();
    }else {
       QMessageBox::critical(this,QString::fromLocal8Bit("提示"),QString::fromLocal8Bit("未输入名册名称!"));
    }



}

void unitrollmake::on_giveup_clicked()
{
    wd = new Widget;

//    wd->show();
//    wd->close();
    QDialog:: reject();
}

void unitrollmake::on_UnitInformation_cellClicked(int row, int column)
{
    ui->CadreInformation->setRowCount(0);
    int i=ui->UnitInformation->currentRow();
    if (i>-1) {
        QString unitname = ui->UnitInformation->item(i,0)->text();
        QString cadreinfo=QString::fromUtf8 ("select  CadreName,CadreInfo.idnum,postname,posttime,rankname,ranktime,birth,sex,AdminAreaCode.AdminArea ,joinworktime from unit,Post,RankCode, CadreInfo left join AdminAreaCode on CadreInfo.NativePlace=AdminAreaCode.AdminAreaID   where unit.unitid=CadreInfo.unitid and CadreInfo.IDNum=Post.IDNum and CadreInfo.RankID=RankCode.RankID and  unit.unitname='%1'").arg(unitname);
        QSqlQuery ci;
        ci.exec(cadreinfo);
        while(ci.next())
        {
            int rowcount=ui->CadreInformation->rowCount();
            ui->CadreInformation->insertRow(rowcount);
            QString p=ci.value(0).toString();
            ui->CadreInformation->setItem(rowcount,0,new QTableWidgetItem(p));

            QString o=ci.value(1).toString();
            ui->CadreInformation->setItem(rowcount,1,new QTableWidgetItem(o));

            QString i=ci.value(2).toString();
            ui->CadreInformation->setItem(rowcount,2,new QTableWidgetItem(i));

            QString u=ci.value(3).toString();
            ui->CadreInformation->setItem(rowcount,3,new QTableWidgetItem(u));
            QString a=ci.value(4).toString();
            ui->CadreInformation->setItem(rowcount,4,new QTableWidgetItem(a));

            QString b=ci.value(5).toString();
            ui->CadreInformation->setItem(rowcount,5,new QTableWidgetItem(b));

            QString e=ci.value(6).toString();
            ui->CadreInformation->setItem(rowcount,6,new QTableWidgetItem(e));


            QString d=ci.value(7).toString();
            if (d.toInt()==1) {
                ui->CadreInformation->setItem(rowcount,7,new QTableWidgetItem("男"));
            }
            if (d.toInt()==0) {
                ui->CadreInformation->setItem(rowcount,7,new QTableWidgetItem("女"));
            }




            QString f=ci.value(8).toString();
            ui->CadreInformation->setItem(rowcount,8,new QTableWidgetItem(f));
            QString g=ci.value(9).toString();
            ui->CadreInformation->setItem(rowcount,9,new QTableWidgetItem(g));
        }

    }
}

void unitrollmake::on_DeleteUnit_clicked()
{
    int r=ui->UnitInformation->currentRow();
    ui->UnitInformation->removeRow(r);
}

void unitrollmake::on_DeleteCadre_clicked()
{
   int r= ui->CadreInformation->currentRow();
   ui->CadreInformation->removeRow(r);
}
//单位及干部的上下移动
void unitrollmake::on_UnitUp_clicked()
{
    int nCurRow = ui->UnitInformation->currentRow();
        moveRow(ui->UnitInformation, nCurRow, nCurRow-1);
}

void unitrollmake::on_UnitDown_clicked()
{
    int nCurRow = ui->UnitInformation->currentRow();
        moveRow(ui->UnitInformation, nCurRow, nCurRow+1);
}

void unitrollmake::on_CadreUp_clicked()
{

    int nCurRow = ui->CadreInformation->currentRow();
        moveRow(ui->CadreInformation, nCurRow, nCurRow-1);
}

void unitrollmake::on_CadreDown_clicked()
{
    int nCurRow = ui->CadreInformation->currentRow();
        moveRow(ui->CadreInformation, nCurRow, nCurRow+1);
}

//表格上下移动函数
void unitrollmake::copyRow( QTableWidget *pTable, int nFrom, int nTo )
{
    int nColCount = pTable->columnCount();
    for(int col=0; col<nColCount; col++){
        QString text = pTable->item(nFrom, col)->text();
        QTableWidgetItem *it = new QTableWidgetItem;
        it->setText(text);
        pTable->setItem(nTo, col, it);
    }
}

void unitrollmake::moveRow( QTableWidget *pTable, int nFrom, int nTo )
{
    if( pTable == 0 ) {
        return;
    }
    if( nFrom == nTo ) {
        return;
    }
    if( nFrom < 0 || nTo < 0 ) {
        return;
    }
    int nRowCount = pTable->rowCount();
    if( nFrom >= nRowCount  || nTo >= nRowCount ) return;

    int nColCur = 0;
    nColCur = pTable->currentColumn();
    QTableWidgetItem *itCur = pTable->currentItem();
    if( 0 != itCur ){
        nColCur = itCur->column();
    }
    int nFromRow = nFrom;
    int nInsertRow = nTo;
    if( nTo < nFrom ){  // Up
        nFromRow = nFrom + 1;
        pTable->insertRow(nTo);
//        this->insertRow(pTable, nTo);
    }else { // Down
        nInsertRow = nTo + 1;
        pTable->insertRow(nInsertRow);
//        this->insertRow(pTable, nInsertRow);
    }
    this->copyRow( pTable, nFromRow, nInsertRow );
//    this->removeRow( pTable, nFromRow );   //删除旧行信息
    pTable->removeRow(nFromRow);

    // 选择之前移动的行
    pTable->selectRow( nInsertRow );
    pTable->setCurrentCell(nTo, nColCur);
}





